package com.uml.umlwork.controller;

import com.uml.umlwork.config.annotation.UserLoginToken;
import com.uml.umlwork.dto.course.select.GetOpenCourseDTO;
import com.uml.umlwork.dto.student.UpdateStudentInfoDTO;
import com.uml.umlwork.entity.Class;
import com.uml.umlwork.entity.Course;
import com.uml.umlwork.entity.CourseSelect;
import com.uml.umlwork.service.ClassService;
import com.uml.umlwork.service.CourseSelectService;
import com.uml.umlwork.service.CourseService;
import com.uml.umlwork.service.StudentService;
import com.uml.umlwork.utils.PageResult;
import com.uml.umlwork.utils.SystemMsgJsonResponse;
import com.uml.umlwork.vo.clazz.ClassVO;
import com.uml.umlwork.vo.course.CourseInfoVO;
import com.uml.umlwork.vo.course.select.CourseSelectVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Siki
 * @date 2020/12/22
 */
@Api(tags = "学生相关模块 - api")
@RequestMapping("/api/v1/umlwork/student")
@RestController
public class StudentController {
    private final StudentService studentService;
    private final ClassService classService;
    private final CourseService courseService;
    private final CourseSelectService courseSelectService;

    public StudentController(StudentService studentService, ClassService classService, CourseService courseService, CourseSelectService courseSelectService) {
        this.studentService = studentService;
        this.classService = classService;
        this.courseService = courseService;
        this.courseSelectService = courseSelectService;
    }

    @PostMapping("/updateInfo")
    @ApiOperation(value = "[学生]-更新个人信息接口", httpMethod = "POST")
    @UserLoginToken
    public SystemMsgJsonResponse updateInfo(@RequestBody @Validated UpdateStudentInfoDTO dto,
                                            @RequestAttribute("userId") String id) {
        studentService.updateInformation(dto, id);
        return SystemMsgJsonResponse.success();
    }

    @GetMapping("/getAllOpenCourse")
    @ApiOperation(value = "[学员] - 查询所有开课的课程", httpMethod = "GET", response = CourseInfoVO.class)
    public SystemMsgJsonResponse getAllOpenCourse(@RequestParam(value = "year", defaultValue = "") String year,
                                                  @RequestParam(value = "season", defaultValue = "") String season,
                                                  @RequestParam(value = "isOpen", defaultValue = "") String isOpen) {
        return SystemMsgJsonResponse.success(courseService.getAllOpenCourse(year, season, isOpen)
                .stream().map(CourseInfoVO::new).collect(Collectors.toList()));
    }

    @GetMapping("/getAllClassesByCourseId/{courseId}")
    @ApiOperation(value = "[学员] - 查询课程的所有开课班", httpMethod = "GET",
            response = ClassVO.class)
    public SystemMsgJsonResponse getAllClassesByCourseId(@PathVariable Integer courseId) {
        return SystemMsgJsonResponse.success(classService.getAllClassesByCourseId(courseId)
                .stream().map(ClassVO::new).collect(Collectors.toList()));
    }

    @GetMapping("/selectClass/{classId}")
    @ApiOperation(value = "[学员] - 选择课程", httpMethod = "GET")
    @UserLoginToken
    public SystemMsgJsonResponse selectClass(
            @RequestAttribute("userId") String id,
            @PathVariable Integer classId) {
        studentService.selectClass(studentService.getStudentIdByUserId(id), classId);
        return SystemMsgJsonResponse.success();
    }

    @GetMapping("/getAllCourseSelect")
    @ApiOperation(value = "[学员] - 查询选课成绩", httpMethod = "GET")
    @UserLoginToken
    public SystemMsgJsonResponse getAllCourseSelects(
            @RequestParam(value = "current", defaultValue = "1") Long current,
            @RequestParam(value = "size", defaultValue = "10") Long size,
            @RequestAttribute("userId") String id) {
        PageResult<CourseSelectVO> result = courseSelectService.getAllCourseSelects(current, size, studentService.getStudentIdByUserId(id));
        return SystemMsgJsonResponse.success(result);
    }
}
